// subcontract/equipment/pages/viewFootprint/index.js
const $api = require('../../../../request/api').API;
import Toast from '@vant/weapp/toast/toast';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		timeShow: false,
		equipId: 0,
		currentTime: '',
		minDate: new Date(2020, 0, 1).getTime(),
		maxDate: new Date(2030, 0, 31).getTime(),
		polyline: [{
			points: [],
			color: '#2ecc71',
			width: 2,
			arrowLine: true,
			borderWidth: 2,
			borderColor: '#000',
		}],
		markers: [],
		infos: []
	},

	onLoad(options) {
		const time = this.timeFilter(new Date())
		this.setData({
			currentTime: time,
			equipId: options.equipId
		})
	},

	onShow() {
		this.getData()
	},

	timeFilter(date) {
		let currentDate = date;
		let year = currentDate.getFullYear(); // 年份
		let month = ('0' + (currentDate.getMonth() + 1)).slice(-2); // 月份，注意月份是从0开始的，所以要加1
		let day = ('0' + currentDate.getDate()).slice(-2); // 日期，使用slice(-2)确保始终是两位数
		return `${year}-${month}-${day}`
	},

	getData() {
		wx.showLoading({
			title: '加载中',
			mask: true,
		})
		$api.getFootprintData({
			equipId: this.data.equipId,
			date: this.data.currentTime
		}).then(res => {
			if (res.data.status == 0) {
				wx.hideLoading()
				let data = res.data.data
				if (data.length > 0) {
					let arr = data.map(item => {
						return {
							latitude: item.latitude,
							longitude: item.longitude,
						}
					})
					let infos = data.map(item => {
						return {
							address: item.address,
							areaName: item.areaName,
							cityName: item.cityName,
							createDate: item.createDate
						}
					})
					const commonCallout = {
						bgColor: "#ffffff",
						borderColor: "",
						borderRadius: 10,
						borderWidth: 2,
						color: "#00a9e3",
						display: "ALWAYS",
						fontSize: 14,
						padding: 3,
						textAlign: "center",
					};
					const markers = [{
						id: 1,
						longitude: arr[0].longitude,
						latitude: arr[0].latitude,
						width: 26,
						height: 30,
						callout: {
							...commonCallout,
							content: "起点",
						},
					}, {
						id: 2,
						longitude: arr[arr.length - 1].longitude,
						latitude: arr[arr.length - 1].latitude,
						width: 26,
						height: 30,
						callout: {
							...commonCallout,
							content: "终点",
						}
					}]
					this.setData({
						infos,
						'polyline[0].points': arr,
						markers
					})
				}else{
					this.setData({
						infos:[],
						'polyline[0].points': [],
						markers:[]
					})
				}
			}else{
				wx.hideLoading()
			}
		}).catch(err=>{
			wx.hideLoading()
			Toast.fail(err.msg)
		})
	},

	selectTime() {
		this.setData({
			timeShow: true
		})
	},

	timeClose() {
		this.setData({
			timeShow: false
		})
	},

	timeConfirm(event) {
		const time = this.timeFilter(new Date(event.detail))
		this.setData({
			currentTime: time,
			timeShow: false
		})
		this.getData()
	},
})